package tltshnik.torrent_to_dropbox;

import java.io.File;

public class torrent_to_dropbox 
{
	/**
	 * @param path_to_folders //path_to_folders[0] - path to download folder, path_to_folders[1] - path to dropbox folder with file separator
	 * @throws InterruptedException 
	 */
	public static void main(String[] path_to_folders) throws InterruptedException 
	{
		if(!path_to_folders[0].endsWith(System.getProperty("file.separator"))) // if there is not file_separator in first path
		{
			path_to_folders[0] += System.getProperty("file.separator");
			System.out.println(path_to_folders[0]);
		}
		if(!path_to_folders[1].endsWith(System.getProperty("file.separator"))) // if there is not file_separator in second path
		{
			path_to_folders[1] += System.getProperty("file.separator");
			System.out.println(path_to_folders[1]);
		}
		
		File download_folder = new File(path_to_folders[0]); // first path exist and it is directory 
		if(!download_folder.exists() || !download_folder.isDirectory())
		{
			System.out.println("Can't find first path");
			System.exit(1);
		}
		
		File dropbox_folder = new File(path_to_folders[1]); // second path exist and it is directory
		if(!dropbox_folder.exists() || !dropbox_folder.isDirectory())
		{
			System.out.println("Can't find second path");
			System.exit(2);
		}
		
		while (true)
		{
			File list_of_files_in_download_folder[] = download_folder.listFiles();
			File new_torrent_file;
			for(int i = 0; i < list_of_files_in_download_folder.length; i++)
			{
				if (list_of_files_in_download_folder[i].getName().endsWith(".torrent"))
				{
					new_torrent_file = new File(path_to_folders[1] + list_of_files_in_download_folder[i].getName());
					System.out.println( list_of_files_in_download_folder[i].renameTo(new_torrent_file) );
				}
			}
			Thread.sleep(5000);
		}
	}
}
